package com.lzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzy.base.BaseInfoProperties;
import com.lzy.enums.YesOrNo;
import com.lzy.mapper.custom.FriendShipMapperCustom;
import com.lzy.mapper.FriendshipMapper;
import com.lzy.pojo.Friendship;
import com.lzy.pojo.vo.ContactsVO;
import com.lzy.service.FriendShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2025-08-26
 */
@Service
public class FriendShipServiceImpl extends BaseInfoProperties implements FriendShipService {


    @Autowired
    private FriendshipMapper friendshipMapper;

    @Autowired
    private FriendShipMapperCustom friendShipMapperCustom;

    @Override
    public Friendship getFriendship(String myId, String friendId) {

        QueryWrapper queryWrapper = new QueryWrapper<>()
                .eq("my_id",myId)
                .eq("friend_id",friendId);
        return friendshipMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ContactsVO> queryMyFriends(String myId,boolean isBlack) {
        Map<String,Object> map = new HashMap<>();
        map.put("mySelfId",myId);
        map.put("isBlack",isBlack);
        List<ContactsVO> contactsVOList = friendShipMapperCustom.queryMyFriends(map);
        return contactsVOList;
    }

    @Transactional
    @Override
    public void updateFriendRemark(String myId, String friendId, String friendRemark) {
        Friendship friendship = new Friendship();
        friendship.setFriendRemark(friendRemark);
        friendship.setUpdatedTime(LocalDateTime.now());

        QueryWrapper queryWrapper = new QueryWrapper<>()
                .eq("my_id",myId)
                .eq("friend_id",friendId);
        friendshipMapper.update(friendship,queryWrapper);
    }
    @Transactional
    @Override
    public void updateBlackList(String myId, String friendId,YesOrNo yesOrNo) {
        Friendship friendship = new Friendship();
        friendship.setIsBlack(yesOrNo.type);
        friendship.setUpdatedTime(LocalDateTime.now());

        QueryWrapper queryWrapper = new QueryWrapper<>()
                .eq("my_id",myId)
                .eq("friend_id",friendId);
        friendshipMapper.update(friendship,queryWrapper);
    }
    @Transactional
    @Override
    public void delete(String myId, String friendId) {
        QueryWrapper<Friendship> friendShipQueryWrapper = new QueryWrapper<>();
        friendShipQueryWrapper.eq("my_id",myId)
                .eq("friend_id",friendId);
        friendshipMapper.delete(friendShipQueryWrapper);

        QueryWrapper<Friendship> friendShipQueryWrapper1 = new QueryWrapper<>();
        friendShipQueryWrapper1.eq("my_id",friendId)
                .eq("friend_id",myId);
        friendshipMapper.delete(friendShipQueryWrapper1);
    }

    @Override
    public boolean isBlackEachOther(String friendId1st, String friendId2nd) {
        QueryWrapper<Friendship> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("my_id", friendId1st);
        queryWrapper1.eq("friend_id", friendId2nd);
        queryWrapper1.eq("is_black", YesOrNo.YES.type);

        Friendship friendship1st = friendshipMapper.selectOne(queryWrapper1);

        QueryWrapper<Friendship> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("my_id", friendId2nd);
        queryWrapper2.eq("friend_id", friendId1st);
        queryWrapper2.eq("is_black", YesOrNo.YES.type);

        Friendship friendship2nd = friendshipMapper.selectOne(queryWrapper2);

        return friendship1st != null || friendship2nd != null;
    }
}
